/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
*/

package plar.core;

import java.util.ArrayList;

import plar.core.Action;

import plar.core.Element;

import plar.core.ElementPlayer;

/**
 * this class handle the input from the user and send it to the Player class
 * a key set is stored in a Byte, see the class KeyFlag
 *
 * @author Antonio Ragagnin
 *
 */
public class ActionUserInput extends Action {

    /**
     * stored keys, this class will repeat them if no new keys are sent.
     */
    KeyFlag currentKeys;

    /**
     * keys received are handled at each run() calls.
     */
    public ArrayList<KeyFlag> keysQueue;

    public KeyFlag popKeys() {
        if (keysQueue.size() == 0)
            return null;
        KeyFlag keys =keysQueue.get(0);
        keysQueue.remove(0);
        return keys;
    }



    public ActionUserInput() {

        currentKeys = new KeyFlag((short)0);
        currentKeys.Key=0;
        keysQueue = new ArrayList<KeyFlag>();
    }

    public void run() {

        ElementPlayer pme = (ElementPlayer)me;
        KeyFlag newKeys = popKeys();
        if(newKeys == null && currentKeys!=null && currentKeys.Key!=KeyFlag.NONE) newKeys=currentKeys;
        if(newKeys == null) return;
        pme.input(newKeys);
        currentKeys = newKeys;
    }

    /**
     * Called by the game when a new keySet is sent. add it to the queue
     * newKeys are managed in the next call of step();
     */
    public Object force(Object o) {
        KeyFlag inputKeys= (KeyFlag)o;
        keysQueue.add(inputKeys);
        return null;
    }


}


